using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using logistical.Models;

namespace logistical.Controllers
{
    public class order_account_hzController : baseController
    {
        public class list_order_account_hz
        {
            public List<order_account_hz> objs { get; set; }
        }

        [HttpGet]
        public object get()
        {
            try
            {
                var o = new logisticalEntities();
                var p = o.order_account_hz;
                var type = o.sys_iflag.Where(x => x.typecode == "accounttype").Select(x => new
                                                {
                                                    id = x.code,
                                                    text = x.name
                                                }).OrderBy(x => x.id);
                var type2 = o.sys_iflag.Where(x => x.typecode == "accountclass").Select(x => new
                {
                    id = x.code,
                    text = x.name
                }).OrderBy(x => x.id);
                var companyid = o.base_company.Where(x => x.companytype.Contains("1") || x.companytype.Contains("3")).Select(x => new
                {
                    id = x.id,
                    text = x.name
                }).OrderBy(x => x.text);

                return new
                {
                    data = p
                    ,
                    type,
                    type2,
                    companyid
                };
            }
            catch (Exception ex)
            {
                return new { msg = ex.Message };
            }
        }

        [HttpGet]
        public object get(string orderid)
        {
            try
            {
                var o = new logisticalEntities();
                var p = o.order_account_hz.Where(x => x.orderid == orderid);
                var type = o.sys_iflag.Where(x => x.typecode == "accounttype").Select(x => new
                {
                    id = x.code,
                    text = x.name
                }).OrderBy(x => x.id);
                var type2 = o.sys_iflag.Where(x => x.typecode == "accountclass").Select(x => new
                {
                    id = x.code,
                    text = x.name
                }).OrderBy(x => x.id);
                var companyid = o.base_company.Where(x => x.companytype.Contains("1") || x.companytype.Contains("3")).Select(x => new
                {
                    id = x.id,
                    text = x.name,
                    rebate = o.base_companyRebate.Where(a => a.companyid == x.id).Select(a => new
                    {
                        code = a.accountclass,
                        a.rebate
                    })
                }).OrderBy(x => x.text);

                var parent = o.order_main.Where(x => x.id == orderid).FirstOrDefault();
                var yunfei = o.base_area.Where(x => (x.id == parent.barea) || (x.id == parent.earea)).FirstOrDefault();
                yunfei.bulky = yunfei.bulky == null ? 0 : yunfei.bulky;
                yunfei.hard = yunfei.hard == null ? 0 : yunfei.hard;
                double? yunfei_val = yunfei.bulky * Convert.ToDouble(parent.volume) > yunfei.hard * Convert.ToDouble(parent.weight) ?
                    yunfei.bulky * Convert.ToDouble(parent.volume) : yunfei.hard * Convert.ToDouble(parent.weight);
                double? baofei_val = parent.tolvalue * 0.0005;
                return new
                {
                    data = p
                ,
                    type,
                    type2,
                    companyid,
                    yunfei_val,
                    baofei_val
                };
            }
            catch (Exception ex)
            {
                return new { msg = ex.Message };
            }
        }

        [HttpPost]
        public object add(order_account_hz obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                obj.id = Guid.NewGuid().ToString();
                common.sysEval.evalCurUserAndDate(obj);
                o.order_account_hz.Add(obj);
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object edit(order_account_hz obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.order_account_hz.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.order_account_hz.SingleOrDefault(x => x.id == obj.id);
                    old.type = obj.type;
                    old.type2 = obj.type2;
                    old.companyid = obj.companyid;
                    old.amount = obj.amount;
                    old.edate = obj.edate;
                    old.filler = obj.filler;
                    old.filldate = obj.filldate;
                    old.remark = obj.remark;
                    old.orderid = obj.orderid;
                    old.preceamount = obj.preceamount;
                    old.rebateamount = obj.rebateamount;
                    common.sysEval.evalCurUserAndDate(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object del(order_account_hz obj)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                var co = o.order_account_hz.Where(x => x.id == obj.id);
                if (co.Count() == 1)
                {
                    var old = o.order_account_hz.SingleOrDefault(x => x.id == obj.id);
                    o.order_account_hz.Remove(old);
                    o.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }

        [HttpPost]
        public object mdel(list_order_account_hz objs)
        {
            string msg = "";
            try
            {
                var o = new logisticalEntities();
                foreach (var p in objs.objs)
                {
                    var co = o.order_account_hz.Where(x => x.id == p.id);
                    if (co.Count() == 1)
                    {
                        var old = o.order_account_hz.SingleOrDefault(x => x.id == p.id);
                        o.order_account_hz.Remove(old);
                    }
                }
                o.SaveChanges();
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return new { msg = msg };
        }
    }
}
